<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        :scroll="{x:true}"
        bordered
        class="j-table-force-nowrap"
        rowKey="id"
        size="middle"
        @change="handleTableChange">


        <span slot="customTitle" slot-scope="text, record">
          <template v-if="record.status === 'DRAFT'">
            <a-icon type="edit" theme="outlined" style="color: #1890ff;" /> 拟稿
          </template>
          <template v-else-if="record.status === 'PUBLISHED'">
            <a-icon type="check-circle" theme="filled" style="color: #52c41a;" /> 已发布
          </template>
          <template v-else-if="record.status === 'np'">
            <a-icon type="clock-circle" theme="outlined" style="color: #f5222d;" /> 未发布
          </template>
        </span>

        <template slot="customTableType" slot-scope="text, record">
          <template v-if="record.tableType === 'MANAGED'">
             内部表
          </template>
          <template v-else-if="record.tableType === 'EXTERNAL'">
             外部表
          </template>
        </template>


        <span slot="action" slot-scope="text, record">+
          <a @click="handleEdit(record)">编辑</a>
           <a-divider type="vertical" />
          <a-popconfirm  title="确定发布吗?" @confirm="() => handlePublish(record)">
                  <a>发布</a>
                </a-popconfirm>
          <a-divider type="vertical" />
          <a v-if="record.status === 'PUBLISHED'" @click="jxcx(record)">即席查询</a>
          <a-divider type="vertical" />
          <a v-if="record.status === 'PUBLISHED'" @click="ddlInfo(record)">源表DDL信息</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <datalib-logic-table-modal ref="modalForm" :currSelectedType="currSelected"
                               @ok="modalFormOk"></datalib-logic-table-modal>
    <datalib-table-j-x-c-x ref="jxcxModal"></datalib-table-j-x-c-x>
    <datalib-table-inner-info ref="tableInfoModal"></datalib-table-inner-info>

  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import DatalibLogicTableModal from './modules/DatalibLogicTableModal'
import DatalibTableJXCX from './modules/DatalibTableJXCX.vue'
import DatalibTableInnerInfo from './modules/DatalibTableInnerInfo.vue'
import { getAction } from '@api/manage'
import StepForm from './stepForm/StepForm.vue'
import { publishTable } from '@/api/apiDatalib'
import error from '@views/result/Error.vue'

export default {
  props: {
    // 声明接收名为currSelected的prop
    currSelected: {
      type: Object, // 根据实际数据类型定义
      required: true // 如果这个prop是必需的，可以设置为true
    }
  },
  name: 'DatalibLogicTableList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    DatalibLogicTableModal,
    StepForm,
    DatalibTableJXCX,
    DatalibTableInnerInfo

  },
  data() {
    return {
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      description: 'datalib_logic_table管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '表名',
          align: 'center',
          dataIndex: 'tableName',
          width: 150,
        },
        {
          title: '表编码',
          align: 'center',
          dataIndex: 'tableCode',
          width: 150,
        },
        {
          title: '表分类',
          align: 'center',
          dataIndex: 'categoryName',
          width: 150,
        },
        {
          title: '表描述',
          align: 'center',
          dataIndex: 'tableDesc'
        },
        {
          title: '表类型',
          align: 'center',
          dataIndex: 'tableType',
          scopedSlots: { customRender: 'customTableType' }
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          width: 60,
          scopedSlots: { customRender: 'customTitle' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/dcloud-platform/api/datalibLogicTable/v1/list',
        delete: '/dcloud-platform/api/datalibLogicTable/v1/delete'
      },
      dictOptions: {},
      superFieldList: [],
      dataSource: []
    }
  },
  watch: {
    currSelected: {
      deep: true,
      immediate: true,
      handler(val) {
        console.log('val', val)
        this.loadData()
      }
    }
  },

  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {
    },
    searchQuery(){
      this.loadData()
    },

    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'tableName', text: '表名;表名-对应表的中文名称' })
      fieldList.push({ type: 'string', value: 'tableCode', text: '表编码;表名-英文' })
      fieldList.push({ type: 'string', value: 'tableDesc', text: '表描述;表描述' })
      fieldList.push({ type: 'string', value: 'tableType', text: '表类型;事实表，维度表' })
      fieldList.push({ type: 'string', value: 'tableSourceId', text: '表数据源id;表数据源id' })
      this.superFieldList = fieldList
    },
    loadData() {  //数据加载
      var params = this.getQueryParams() //查询条件
      params['categoryId'] = this.currSelected.id
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    handleAdd: function() {
      if (this.currSelected.categoryCode == null) {
        this.$message.error('请先选择数据分类！')
        return
      }
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    handleEdit: function(record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑'
    },
    handlePublish: function(record) {
      let param = {}
      param["tableId"] = record.id
      publishTable(param).then(res => {
          if (res.result) {
            this.$message.info("发布成功")
          }else{
            this.$message.error("发布失败")
          }
      }).catch(err => {
        this.$message.error("发布失败,原因是"+err);
      })

      this.loadData()

    },
    handleDetail: function(record){
      this.$refs.modalForm.info(record)
      this.$refs.modalForm.title = '详情'
    },
    jxcx:function(record){
      this.$refs.jxcxModal.open(record)
    },
    ddlInfo(record){
      this.$refs.tableInfoModal.open(record)
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>